<?php

class Emails_model extends Model {

    public function listaEmails($inicio,$limite)
    {
        try {
            $count = "SELECT count(*) as Total
                      FROM nws_email"; 
            $rs['count'] = Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc($count);
            
            $query = "SELECT e.* , s.segNom
                      FROM nws_email e
                      INNER JOIN nws_email_segmento s ON (s.segCod = e.emlSeg)
                      ORDER BY emlDatIns DESC
                      LIMIT ".$inicio.",".$rs['count'][0]['Total'];
            $rs['lista'] = Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc($query);
            return $rs;

        } catch (Doctrine_Exception $e) {
            print $e->getMessage();
        }   
    }
  	// Listagem Segmentos Emails
    public function SegmentosEmail()
	{      
        try {
            $seg = Doctrine::getTable('NwsEmailSegmento')->findAll();
            return $seg;
            
        } catch (Doctrine_Exception $e) {
            print $e->getMessage();
        }
	}
    
    public function inserirEmail()
    {
        $emailNovo = new NwsEmail;
        $emailNovo->setemlNom($_POST['emlNom']);
        $emailNovo->setemlSta($_POST['emlSta']);
        $emailNovo->setemlMail($_POST['emlMail']);
        $emailNovo->setemlRemCod(123);
        $emailNovo->setemlSeg($_POST['emlSeg']);
        $emailNovo->setemlDatIns(date('Y-m-d H:i'));
        $emailNovo->insertEmail();
        header('location:'. BASE_URL_ADM . 'emails');
    }
    
    public function excluirEmail($emlCod)
    {
        $email = Doctrine::getTable('NwsEmail')->find($emlCod);
        if($email) {
            $email->delete();
            header('location:'. BASE_URL_ADM . 'emails');
        } else {
            echo "errado";
        }
    }
    
    public function editaEmail($emlCod)
    {
        $email = Doctrine::getTable('NwsEmail')->find($emlCod);
        return $email;
    }
    
    public function editaEmaildb($post)
    {
        $bannersloc = Doctrine_Query::create()
        ->update('NwsEmail')
        ->set('emlNom', "'".$post['emlNom']."'")
        ->set('emlMail', "'".$post['emlMail']."'")
        ->set('emlSta', "'".$post['emlSta']."'")
        ->set('emlSeg', "'".$post['emlSeg']."'")
        ->where('emlCod = '.$post['emlCod'] );
        $bannersloc->execute();
        header('location:'. BASE_URL_ADM . 'emails/');
    }
    
    public function inserirSegmento()
    {
        $segmento = new NwsEmailSegmento;
        $segmento->setsegNom($_POST['segNom']);
        $segmento->setsegCodPai($_POST['segCodPai']);
        $segmento->insertSegmento();
        header('location:'. BASE_URL_ADM . 'emails/segmentos/');
    }
    
    public function editaSegmento($segCod)
    {
        $segmentos = Doctrine::getTable('NwsEmailSegmento')->find($segCod);
        return $segmentos;
    }
    
    public function editaSegmentodb($post)
    {
        $segmentos = Doctrine_Query::create()
        ->update('NwsEmailSegmento')
        ->set('segCodPai', "'".$post['segCodPai']."'")
        ->set('segNom', "'".$post['segNom']."'")
        ->where('segCod = '.$post['segCod'] );
        $segmentos->execute();
        header('location:'. BASE_URL_ADM . 'emails/segmentos/');
    }
    
    public function excluirSegmento($segCod)
    {
        $segmento = Doctrine::getTable('NwsEmailSegmento')->find($segCod);
        if($segmento) {
            $segmento->delete();
            header('location:'. BASE_URL_ADM . 'emails/segmentos/');
        } else {
            echo "errado";
        }
    }
    
    public function trocaStatus($emlCod,$emlSta)
    {
        $status = Doctrine_Query::create()
        ->update('NwsEmail')
        ->set('emlSta', "'".$emlSta."'")
        ->where('emlCod = '.$emlCod);
        $status->execute();
        header('location:'. BASE_URL_ADM . 'emails/');
    }
}

?>
